﻿using HlELock.Core;
using HlELock.Core.Events;
using Prism.Events;
using Prism.Regions;
using Syncfusion.Windows.Shared;
using System.Linq;
using System.Windows;

namespace HlELock.Page.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : ChromelessWindow
    {
        private readonly IRegionManager _regionManager;
        public MainWindow(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }
        private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _regionManager.RequestNavigate(RegionNames.HotelRegion, nameof(LoginPage));
            EventBus.Instace.GetEvent<DialogWindowEvent>().Subscribe(args =>
            {
                var dialog = new DialogWindow(_regionManager, this, args);
                var dialogRegion = _regionManager.Regions.FirstOrDefault(f => f.Name == RegionNames.DialogWindowRegion);
                if (dialogRegion != null)
                {
                    _regionManager.Regions.Remove(RegionNames.DialogWindowRegion);
                }
                RegionManager.SetRegionManager(dialog, _regionManager);
                mask.Visibility = System.Windows.Visibility.Visible;
                dialog.Show();
                mask.Visibility = System.Windows.Visibility.Collapsed;
                if (MyAppContext.Device == null)
                {
                    var d = AppHelper.GetResourceString("MeiYouZhaoDaoSheBei");
                    //MessageBox.Show(d);
#if !DEBUG
                    Application.Current.Shutdown();
#endif
                }
            }, ThreadOption.UIThread);
            EventBus.Instace.GetEvent<DialogWindowBusyEvent>().Subscribe(args =>
            {
                DialogWindowViewModel.Instance.IsBusy = args.IsBusy;
                DialogWindowViewModel.Instance.Descr = args.Descr;
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var window = new DoorLockWindow();
            window.ShowDialog();
        }
    }
}
